package com.cskaoyan.wordmemorize.service.impl;

import com.alibaba.excel.EasyExcel;
import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.upload.AliOssProperties;
import com.cskaoyan.wordmemorize.common.util.FileNameGenerator;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.*;
import com.cskaoyan.wordmemorize.dao.mapper.*;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.dto.app.DisplayBookVocDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayChapterDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayVocInterpretationDTO;
import com.cskaoyan.wordmemorize.dto.app.UserBookStatisticsDTO;
import com.cskaoyan.wordmemorize.listener.VocFileExcelDataListener;
import com.cskaoyan.wordmemorize.listener.VocSingleExcelValidListener;
import com.cskaoyan.wordmemorize.request.*;
import com.cskaoyan.wordmemorize.service.VocService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VocServiceImpl implements VocService {

    @Resource
    VocConverter vocConverter;

    @Autowired
    VocMapper vocMapper;

    @Autowired
    VocChapBookMapper vocChapBookMapper;

    @Autowired
    VocMeaningMapper vocMeaningMapper;


    @Autowired
    ExampleSentenceMapper exampleSentenceMapper;


    @Autowired
    VocBookMapper vocBookMapper;


    @Autowired
    VocChapterMapper vocChapterMapper;

    @Autowired
    UserFavoriteMapper favoriteMapper;

    @Autowired
    UserNotRememberMapper userNotRememberMapper;

    @Autowired
    UserVocRevMapper userVocRevMapper;

    @Autowired
    UserVocExampleSentenceMapper userVocExampleSentenceMapper;

    @Autowired
    OSS oss;

    @Autowired
    AliOssProperties aliOssProperties;

    @Override
    public PageDTO<VocDTO> getVocabularies(VocPageRequest pageQuery) {

        // 分页查询
        int pageNum = pageQuery.getPageNum();
        int pageSize = pageQuery.getCount();
        int offset = (pageNum - 1) * pageSize;

        Page<VocChapterDO> vocDOPage = new Page<>(pageNum, pageSize);
        Page<VocChapterDO> vocChapterDOPage = vocChapterMapper.selectPage(vocDOPage, null);
        List<VocChapterDO> records = vocChapterDOPage.getRecords();

        Page<VocDO> vocabularyInfo = vocMapper.getVocabularyInfo(pageQuery.getBookId() == null ? null : Long.valueOf(pageQuery.getBookId()), pageQuery.getChapterId() == null ? null : Long.valueOf(pageQuery.getChapterId()), pageQuery.getKeyword(), new Page<>(pageNum, pageSize));
        PageDTO<VocDTO> pageDTO = new PageDTO<>();
        pageDTO.setPageList(new ArrayList<>());
        for (VocDO record : vocabularyInfo.getRecords()) {
            VocDTO vocDTO = vocConverter.vocDO2DTO(record);
            pageDTO.getPageList().add(vocDTO);
        }
        pageDTO.setTotalCount(vocabularyInfo.getTotal());
        return pageDTO;
    }

    @Override
    @Transactional
    public void addVoc(VocCommand command) {

        // 将单词的基本信息保存到voc表中
        VocDO vocDO = vocConverter.command2VocDO(command);
        vocMapper.insert(vocDO);

        // 保存关系
        List<VocChapBookDO> relations = vocConverter.commands2VocChapBookDOs(command.getRelations());
        if (relations != null) {
            for (VocChapBookDO vocChapBookDO : relations) {
                // 设置单词id
                vocChapBookDO.setVocId(vocDO.getId());
                vocChapBookMapper.insert(vocChapBookDO);

                // 修改书籍和章节关联的单词数量
                if (vocChapBookDO.getChapterId() != null) {
                    // 关联到了一个章节
                    LambdaUpdateWrapper<VocChapterDO> vocChapterWrapper = new LambdaUpdateWrapper<>();
                    vocChapterWrapper.eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                            // set voc_count = voc_count + 1
                            .setSql("voc_count = voc_count + 1");

                    vocChapterMapper.update(null, vocChapterWrapper);
                }


                // 更新书籍关联的单词数量
                // 关联到了一个章节
                LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
                vocBookWrapper.eq(VocBookDO::getId, vocChapBookDO.getBookId())
                        // set voc_count = voc_count + 1
                        .setSql("voc_count = voc_count + 1");
                vocBookMapper.update(null, vocBookWrapper);

            }
        }


        // 保存释义
        List<VocMeaningCommand> interpretations = command.getInterpretations();
        if (interpretations != null) {
            List<VocMeaningDO> vocMeaningDOs = vocConverter.commands2VocMeaningDOs(interpretations);
            for (VocMeaningDO vocMeaningDO : vocMeaningDOs) {
                vocMeaningDO.setVocId(vocDO.getId());
                vocMeaningMapper.insert(vocMeaningDO);

            }
        }


        // 保存例句
        List<ExampleSentenceCommand> examples = command.getExamples();
        if (examples != null) {
            List<ExampleSentenceDO> exampleSentenceDOS = vocConverter.commands2ExampleSentenceDOs(examples);
            for (ExampleSentenceDO exampleSentenceDO : exampleSentenceDOS) {
                exampleSentenceDO.setVocId(vocDO.getId());
                exampleSentenceMapper.insert(exampleSentenceDO);
            }
        }


    }

    @Override
    public void updateVoc(VocCommand command) {

        // 将单词的基本信息保存到voc表中
        VocDO vocDO = vocConverter.command2VocDO(command);
        LambdaUpdateWrapper<VocDO> vocWrapper = new LambdaUpdateWrapper<>();
        vocWrapper.eq(VocDO::getId, vocDO.getId());
        vocMapper.update(vocDO, vocWrapper);


        // 保存关系
        List<VocChapBookDO> relations = vocConverter.commands2VocChapBookDOs(command.getRelations());
        if (relations != null) {
            for (VocChapBookDO vocChapBookDO : relations) {
                if (vocChapBookDO.getId() != null) {
                    // 有id，说明是修改
                    LambdaUpdateWrapper<VocChapBookDO> vocChapBookWrapper = new LambdaUpdateWrapper<>();
                    vocChapBookWrapper.eq(VocChapBookDO::getId, vocChapBookDO.getId());
                    vocChapBookMapper.update(vocChapBookDO, vocChapBookWrapper);
                    continue;
                }
                // 设置单词id
                vocChapBookDO.setVocId(vocDO.getId());
                vocChapBookMapper.insert(vocChapBookDO);

                // 修改书籍和章节关联的单词数量
                if (vocChapBookDO.getChapterId() != null) {
                    // 关联到了一个章节
                    LambdaUpdateWrapper<VocChapterDO> vocChapterWrapper = new LambdaUpdateWrapper<>();
                    vocChapterWrapper.eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                            // set voc_count = voc_count + 1
                            .setSql("voc_count = voc_count + 1");

                    vocChapterMapper.update(null, vocChapterWrapper);
                }


                // 更新书籍关联的单词数量
                // 关联到了一本书
                LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
                vocBookWrapper.eq(VocBookDO::getId, vocChapBookDO.getBookId())
                        // set voc_count = voc_count + 1
                        .setSql("voc_count = voc_count + 1");
                vocBookMapper.update(null, vocBookWrapper);

            }
        }


        // 保存释义
        List<VocMeaningCommand> interpretations = command.getInterpretations();
        if (interpretations != null) {
            List<VocMeaningDO> vocMeaningDOs = vocConverter.commands2VocMeaningDOs(interpretations);
            for (VocMeaningDO vocMeaningDO : vocMeaningDOs) {
                if (vocMeaningDO.getId() != null) {
                    // 有id，说明是修改
                    LambdaUpdateWrapper<VocMeaningDO> vocMeaningWrapper = new LambdaUpdateWrapper<>();
                    vocMeaningWrapper.eq(VocMeaningDO::getId, vocMeaningDO.getId());
                    vocMeaningMapper.update(vocMeaningDO, vocMeaningWrapper);
                    continue;
                }
                vocMeaningDO.setVocId(vocDO.getId());
                vocMeaningMapper.insert(vocMeaningDO);

            }
        }


        // 保存例句
        List<ExampleSentenceCommand> examples = command.getExamples();
        if (examples != null) {
            List<ExampleSentenceDO> exampleSentenceDOS = vocConverter.commands2ExampleSentenceDOs(examples);
            for (ExampleSentenceDO exampleSentenceDO : exampleSentenceDOS) {
                if (exampleSentenceDO.getId() != null) {
                    // 有id，说明是修改
                    LambdaUpdateWrapper<ExampleSentenceDO> exampleSentenceWrapper = new LambdaUpdateWrapper<>();
                    exampleSentenceWrapper.eq(ExampleSentenceDO::getId, exampleSentenceDO.getId());
                    exampleSentenceMapper.update(exampleSentenceDO, exampleSentenceWrapper);
                    continue;
                }
                exampleSentenceDO.setVocId(vocDO.getId());
                exampleSentenceMapper.insert(exampleSentenceDO);
            }
        }
    }

    @Override
    public DeleteVocOperateDTO deleteVocabulary(Long vocId) {
        DeleteVocOperateDTO deleteVocOperateDTO = new DeleteVocOperateDTO();
        deleteVocOperateDTO.setId(Long.valueOf(vocId));
        deleteVocOperateDTO.setContent(vocMapper.selectById(vocId).getContent());
        // 删除单词
        vocMapper.deleteById(vocId);
        // 删除单词-章节-书籍关系
        LambdaUpdateWrapper<VocChapBookDO> vocChapBookWrapper = new LambdaUpdateWrapper<>();
        vocChapBookWrapper.eq(VocChapBookDO::getVocId, vocId);
        vocChapBookMapper.delete(vocChapBookWrapper);

        // 修改书籍和章节关联的单词数量
        LambdaQueryWrapper<VocChapBookDO> vocChapBookQueryWrapper = new LambdaQueryWrapper<>();
        vocChapBookQueryWrapper.eq(VocChapBookDO::getVocId, vocId);
        List<VocChapBookDO> vocChapBookDOs = vocChapBookMapper.selectList(vocChapBookQueryWrapper);
        for (VocChapBookDO vocChapBookDO : vocChapBookDOs) {
            if (vocChapBookDO.getChapterId() != null) {
                // 关联到了一个章节
                LambdaUpdateWrapper<VocChapterDO> vocChapterWrapper = new LambdaUpdateWrapper<>();
                vocChapterWrapper.eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                        // set voc_count = voc_count - 1
                        .setSql("voc_count = voc_count - 1");

                vocChapterMapper.update(null, vocChapterWrapper);
            }
            if (vocChapBookDO.getBookId() != null) {
                // 关联到了一本书
                LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
                vocBookWrapper.eq(VocBookDO::getId, vocChapBookDO.getBookId())
                        // set voc_count = voc_count - 1
                        .setSql("voc_count = voc_count - 1");
                vocBookMapper.update(null, vocBookWrapper);
            }
        }


        // 删除单词释义
        LambdaUpdateWrapper<VocMeaningDO> vocMeaningWrapper = new LambdaUpdateWrapper<>();
        vocMeaningWrapper.eq(VocMeaningDO::getVocId, vocId);
        vocMeaningMapper.delete(vocMeaningWrapper);

        // 删除单词例句
        LambdaUpdateWrapper<ExampleSentenceDO> exampleSentenceWrapper = new LambdaUpdateWrapper<>();
        exampleSentenceWrapper.eq(ExampleSentenceDO::getVocId, vocId);
        exampleSentenceMapper.delete(exampleSentenceWrapper);
        return deleteVocOperateDTO;
    }

    @Override
    public List<DeleteVocOperateDTO> deleteVocabularies(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        List<DeleteVocOperateDTO> deleteVocOperateDTOList = new ArrayList<>();
        for (String id : ids) {
            DeleteVocOperateDTO deleteVocOperateDTO = deleteVocabulary(Long.valueOf(id));
            deleteVocOperateDTOList.add(deleteVocOperateDTO);
        }
        return deleteVocOperateDTOList;
    }

    @Override
    public List<VocChapBookDTO> getVocRelations(Long vocId) {

        // 查询单词-章节-书籍关系
        LambdaQueryWrapper<VocChapBookDO> vocChapBookQueryWrapper = new LambdaQueryWrapper<>();
        vocChapBookQueryWrapper.eq(VocChapBookDO::getVocId, vocId);
        List<VocChapBookDO> vocChapBookDOs = vocChapBookMapper.selectList(vocChapBookQueryWrapper);
        List<VocChapBookDTO> vocChapBookDTOs = new ArrayList<>();
        for (VocChapBookDO vocChapBookDO : vocChapBookDOs) {
            VocChapBookDTO vocChapBookDTO = new VocChapBookDTO();

            vocChapBookDTO.setId(String.valueOf(vocChapBookDO.getId()));
            vocChapBookDTO.setVocId(String.valueOf(vocChapBookDO.getVocId()));
            vocChapBookDTO.setVocName(vocChapBookDO.getVocName());
            if (vocChapBookDO.getBookId() != null) {
                vocChapBookDTO.setBookId(String.valueOf(vocChapBookDO.getBookId()));
                vocChapBookDTO.setBookName(vocChapBookDO.getBookName());
            }
            if (vocChapBookDO.getBookId() != null) {
                QueryWrapper<VocBookDO> wrapper = new QueryWrapper<>();
                wrapper.eq("id", vocChapBookDO.getBookId());
                vocChapBookDTO.setCategoryId(String.valueOf(vocBookMapper.selectOne(wrapper).getBcId()));
                vocChapBookDTO.setCategoryName(vocBookMapper.selectOne(wrapper).getBcName());
            }
            if (vocChapBookDO.getChapterId() != null) {
                vocChapBookDTO.setChapterId(String.valueOf(vocChapBookDO.getChapterId()));
                vocChapBookDTO.setChapterName(vocChapBookDO.getChapterName());
            }

            vocChapBookDTOs.add(vocChapBookDTO);
        }
        return vocChapBookDTOs;
    }

    @Override
    public PageDTO<DisplayBookVocDTO> getDisplayBookVocsInfo(AppVocPageRequest pageRequest) {
        String bookId = pageRequest.getBookId();
        QueryWrapper<VocChapBookDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookId);
        List<VocChapBookDO> vocChapBookDOs = vocChapBookMapper.selectList(queryWrapper);
        List<VocDO> vocDOS = new ArrayList<>();
        for (VocChapBookDO vocChapBookDO : vocChapBookDOs) {
            VocDO vocDO = new VocDO();
            vocDO.setId(vocChapBookDO.getVocId());
            vocDO.setContent(vocChapBookDO.getVocName());
            vocDOS.add(vocDO);
        }
        System.out.println("qwer"+vocDOS.size());
        PageDTO<DisplayBookVocDTO> displayBookVocDTOPageDTO = vocConverter.displayBookVocList2Page(vocDOS,Long.parseLong(String.valueOf(vocDOS.size())));
        //查看单词的释义
        System.out.println(displayBookVocDTOPageDTO.getPageList().size());
        for(DisplayBookVocDTO displayBookVocDTO:displayBookVocDTOPageDTO.getPageList()) {
            QueryWrapper<VocMeaningDO> vocMeaningDOQueryWrapper = new QueryWrapper<>();
            vocMeaningDOQueryWrapper.eq("voc_id", displayBookVocDTO.getId());
            List<VocMeaningDO> vocMeaningDOs = vocMeaningMapper.selectList(vocMeaningDOQueryWrapper);
            List<DisplayVocInterpretationDTO> displayVocInterpretationDTOS = new ArrayList<>();
            for (VocMeaningDO vocMeaningDO : vocMeaningDOs) {
                DisplayVocInterpretationDTO displayVocInterpretationDTO = vocConverter.vocMeaningDO2DisplayVocInterpretationDTO(vocMeaningDO);
                displayVocInterpretationDTOS.add(displayVocInterpretationDTO);
            }
            displayBookVocDTO.setInterpretations(displayVocInterpretationDTOS);
            //是否已选单词
            QueryWrapper<UserVocRevDO> userVocRevDOQueryWrapper = new QueryWrapper<>();
            userVocRevDOQueryWrapper.eq("voc_id", displayBookVocDTO.getId())
                    .eq("user_id", pageRequest.getUserId());
            UserVocRevDO userVocRevDO = userVocRevMapper.selectOne(userVocRevDOQueryWrapper);
            if(userVocRevDO==null) displayBookVocDTO.setReviewed(0);
            else displayBookVocDTO.setReviewed(userVocRevDO.getType());
            //是否不背
            QueryWrapper<UserNotRememberDO> userNotRememberDOQueryWrapper = new QueryWrapper<>();
            userNotRememberDOQueryWrapper.eq("voc_id", displayBookVocDTO.getId())
                    .eq("user_id", pageRequest.getUserId());
            UserNotRememberDO userNotRememberDO = userNotRememberMapper.selectOne(userNotRememberDOQueryWrapper);
            if(userNotRememberDO==null) displayBookVocDTO.setNotRemember(0);
            else displayBookVocDTO.setNotRemember(1);
        }
        return displayBookVocDTOPageDTO;
    }

    @Override
    public PageDTO<DisplayChapterDTO> getDisplayBookChapterVocsInfo(AppVocPageRequest pageRequest) {
        String bookId = pageRequest.getBookId();
        QueryWrapper<VocChapterDO> vocChapterDOQueryWrapper = new QueryWrapper<>();
        vocChapterDOQueryWrapper.eq("book_id", bookId);
        List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(vocChapterDOQueryWrapper);
//        System.out.println("qwer"+vocDOS.size());
        //所有章节
        PageDTO<DisplayChapterDTO> displayChapterDTOPageDTO = vocConverter.displayChapterVocList2Page(vocChapterDOS, Long.parseLong(String.valueOf(vocChapterDOS.size())));
        //根据章节id和书籍id查询章节下的单词
        for(DisplayChapterDTO displayChapterDTO:displayChapterDTOPageDTO.getPageList()) {
            QueryWrapper<VocChapBookDO> vocChapBookDOQueryWrapper = new QueryWrapper<>();
            vocChapBookDOQueryWrapper.eq("chapter_id", displayChapterDTO.getId())
                            .eq("book_id", bookId);
            List<VocChapBookDO> vocChapBookDOS = vocChapBookMapper.selectList(vocChapBookDOQueryWrapper);
            List<VocDO> vocDOS = new ArrayList<>();
            for(VocChapBookDO vocChapBookDO : vocChapBookDOS) {
                VocDO vocDO = new VocDO();
                vocDO.setId(vocChapBookDO.getVocId());
                vocDO.setContent(vocChapBookDO.getVocName());
                vocDOS.add(vocDO);
            }
            List<DisplayBookVocDTO> displayBookVocDTOS = vocConverter.vocDos2DisplayChapterVocDTOs(vocDOS);
            for (DisplayBookVocDTO displayBookVocDTO : displayBookVocDTOS) {
                QueryWrapper<VocMeaningDO> vocMeaningDOQueryWrapper = new QueryWrapper<>();
                vocMeaningDOQueryWrapper.eq("voc_id",displayBookVocDTO.getId());
                List<VocMeaningDO> vocMeaningDOs = vocMeaningMapper.selectList(vocMeaningDOQueryWrapper);
                List<DisplayVocInterpretationDTO> displayVocInterpretationDTOS = new ArrayList<>();
                for (VocMeaningDO vocMeaningDO : vocMeaningDOs) {
                    DisplayVocInterpretationDTO displayVocInterpretationDTO = vocConverter.vocMeaningDO2DisplayVocInterpretationDTO(vocMeaningDO);
                    displayVocInterpretationDTOS.add(displayVocInterpretationDTO);
                }
                displayBookVocDTO.setInterpretations(displayVocInterpretationDTOS);
                //是否已选单词
                QueryWrapper<UserVocRevDO> userVocRevDOQueryWrapper = new QueryWrapper<>();
                userVocRevDOQueryWrapper.eq("voc_id", displayBookVocDTO.getId())
                        .eq("user_id", pageRequest.getUserId());
                UserVocRevDO userVocRevDO = userVocRevMapper.selectOne(userVocRevDOQueryWrapper);
                if(userVocRevDO==null) displayBookVocDTO.setReviewed(0);
                else displayBookVocDTO.setReviewed(userVocRevDO.getType());
                //是否不背
                QueryWrapper<UserNotRememberDO> userNotRememberDOQueryWrapper = new QueryWrapper<>();
                userNotRememberDOQueryWrapper.eq("voc_id", displayBookVocDTO.getId())
                        .eq("user_id", pageRequest.getUserId());
                UserNotRememberDO userNotRememberDO = userNotRememberMapper.selectOne(userNotRememberDOQueryWrapper);
                if(userNotRememberDO==null) displayBookVocDTO.setNotRemember(0);
                else displayBookVocDTO.setNotRemember(1);
            }
            displayChapterDTO.setDisplayVocs(displayBookVocDTOS);
        }
        return displayChapterDTOPageDTO;
    }

    @Override
    public VocDTO getVocDetail(Long vocId, Long userId) {
        QueryWrapper<VocDO> vocDOQueryWrapper = new QueryWrapper<>();
        vocDOQueryWrapper.eq("id", vocId);
        VocDO vocDO = vocMapper.selectOne(vocDOQueryWrapper);
        //单词是否已选
        QueryWrapper<UserVocRevDO> userVocRevDOQueryWrapper = new QueryWrapper<>();
        userVocRevDOQueryWrapper.eq("voc_id", vocId)
                .eq("user_id", userId);
        UserVocRevDO userVocRevDO = userVocRevMapper.selectOne(userVocRevDOQueryWrapper);
        if(userVocRevDO!=null) vocDO.setReviewed(userVocRevDO.getType());
        //单词是否不背
        QueryWrapper<UserNotRememberDO> userNotRememberDOQueryWrapper = new QueryWrapper<>();
        userNotRememberDOQueryWrapper.eq("voc_id", vocId)
                .eq("user_id", userId);
        UserNotRememberDO userNotRememberDO = userNotRememberMapper.selectOne(userNotRememberDOQueryWrapper);
        if(userNotRememberDO==null) vocDO.setNotRemember(0);
        else vocDO.setNotRemember(1);
        //单词是否已收藏
        QueryWrapper<UserFavoriteDO> userFavoriteDOQueryWrapper = new QueryWrapper<>();
        userFavoriteDOQueryWrapper.eq("voc_id", vocId);
        UserFavoriteDO userFavoriteDO = favoriteMapper.selectOne(userFavoriteDOQueryWrapper);
        if(userFavoriteDO!=null) vocDO.setFavorite(userFavoriteDO.getSelected());
        //单词释义
        QueryWrapper<VocMeaningDO> vocMeaningDOQueryWrapper = new QueryWrapper<>();
        vocMeaningDOQueryWrapper.eq("voc_id", vocId);
        List<VocMeaningDO> vocMeaningDOs = vocMeaningMapper.selectList(vocMeaningDOQueryWrapper);
        vocDO.setInterpretations(vocMeaningDOs);
        //单词例句
        QueryWrapper<ExampleSentenceDO> exampleSentenceDOQueryWrapper = new QueryWrapper<>();
        exampleSentenceDOQueryWrapper.eq("voc_id", vocId);
        List<ExampleSentenceDO> exampleSentenceDOs = exampleSentenceMapper.selectList(exampleSentenceDOQueryWrapper);
        vocDO.setExamples(exampleSentenceDOs);
        //用户自建例句
        QueryWrapper<UserVocExampleSentenceDO> userVocExampleSentenceDOQueryWrapper = new QueryWrapper<>();
        userVocExampleSentenceDOQueryWrapper.eq("voc_id", vocId)
                .eq("user_id", userId);
        List<UserVocExampleSentenceDO> userVocExampleSentenceDOs = userVocExampleSentenceMapper.selectList(userVocExampleSentenceDOQueryWrapper);
        vocDO.setUserExamples(userVocExampleSentenceDOs);
        return vocConverter.vocDO2DTO(vocDO);
    }

    @Override
    public UserBookStatisticsDTO getUserChooseCountInBook(Long userId, Long bookId) {

        return null;
    }

    @Override
    public void vocExcelValidate(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), VocExcelDTO.class, new VocSingleExcelValidListener(vocBookMapper, vocChapBookMapper))
                // 指定读取的sheet我们的一个excel只使用默认的一个sheet所以不用指定
                .sheet()
                // 开始读取,方法执行完毕，那么整个Excel文件也读取完了
                .doRead();

    }

    @Override
    public ExcelAddOperateDTO addVocExcel(MultipartFile file, Long userId) throws IOException {
        ExcelAddOperateDTO excelAddOperateDTO = new ExcelAddOperateDTO();
        EasyExcel.read(file.getInputStream(), VocExcelDTO.class, new VocFileExcelDataListener(vocConverter, vocChapBookMapper, vocChapterMapper, vocBookMapper, vocMapper, vocMeaningMapper, exampleSentenceMapper))
                // 指定读取的sheet我们的一个excel只使用默认的一个sheet所以不用指定
                .sheet()
                // 开始读取,方法执行完毕，那么整个Excel文件也读取完了
                .doRead();
        excelAddOperateDTO.setFileName(file.getOriginalFilename());
        excelAddOperateDTO.setTotalCount(0);
        return excelAddOperateDTO;
    }

    @Override
    public DeleteVocFileOperateDTO deleteFile(Long id, String type, String name) {
        DeleteVocFileOperateDTO deleteDto = new DeleteVocFileOperateDTO();
        deleteDto.setVocId(id);
        deleteDto.setContent(vocMapper.selectById(id).getContent());
        deleteDto.setType(type);
        deleteDto.setFileName(name);
        // 删除数据库中的文件信息,单词音频文件
        LambdaUpdateWrapper<VocDO> vocWrapper = new LambdaUpdateWrapper<>();
        vocWrapper.eq(VocDO::getId, id);
        if (type.equals("uk")) {
            vocWrapper.set(VocDO::getUkPronounciation, null);
        } else if (type.equals("us")) {
            vocWrapper.set(VocDO::getUsPronounciation, null);
        }
        vocMapper.update(null, vocWrapper);

        return deleteDto;

    }

    @Override
    public DeleteVocRelationOperateDTO deleteVocRelation(Long id) {
        DeleteVocRelationOperateDTO dto = new DeleteVocRelationOperateDTO();
        LambdaUpdateWrapper<VocChapBookDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VocChapBookDO::getId, id);


        dto.setId(id);
        dto.setVocId(vocChapBookMapper.selectById(id).getVocId());
        dto.setVocName(vocMapper.selectById(vocChapBookMapper.selectById(id).getVocId()).getContent());
        dto.setBookId(vocChapBookMapper.selectById(id).getBookId());
        if (vocChapBookMapper.selectById(id).getBookId() != null) {
            dto.setBookName(vocBookMapper.selectById(vocChapBookMapper.selectById(id).getBookId()).getName());
            LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
            vocBookWrapper.eq(VocBookDO::getId, dto.getBookId())
                    // set voc_count = voc_count - 1
                    .setSql("voc_count = voc_count - 1");

            vocBookMapper.update(null, vocBookWrapper);
        }
        dto.setChapterId(vocChapBookMapper.selectById(id).getChapterId());
        if (vocChapBookMapper.selectById(id).getChapterId() != null) {
            if (vocChapterMapper.selectById(vocChapBookMapper.selectById(id).getChapterId()) != null) {

                dto.setChapterName(vocChapterMapper.selectById(vocChapBookMapper.selectById(id).getChapterId()).getChapterName());
            }
            LambdaUpdateWrapper<VocChapterDO> vocChapterWrapper = new LambdaUpdateWrapper<>();
            vocChapterWrapper.eq(VocChapterDO::getId, dto.getChapterId())
                    // set voc_count = voc_count - 1
                    .setSql("voc_count = voc_count - 1");
            vocChapterMapper.update(null, vocChapterWrapper);
        }

        // 删除单词-章节-书籍关系
        LambdaUpdateWrapper<VocChapBookDO> vocChapBookWrapper = new LambdaUpdateWrapper<>();
        vocChapBookWrapper.eq(VocChapBookDO::getId, id);
        int delete = vocChapBookMapper.delete(vocChapBookWrapper);
        if (delete > 0) {
            return dto;
        }
        return null;
    }

    @Override
    public DeleteInterpretationOperateDTO deleteInterpretation(Long id) {
        DeleteInterpretationOperateDTO dto = new DeleteInterpretationOperateDTO();
        LambdaUpdateWrapper<VocMeaningDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VocMeaningDO::getId, id);
        dto.setId(id);
        dto.setType(vocMeaningMapper.selectById(id).getType());
        dto.setInterContent(vocMeaningMapper.selectById(id).getInterContent());
        dto.setVocId(vocMapper.selectById(vocMeaningMapper.selectById(id).getVocId()).getId());
        dto.setVocName(vocMapper.selectById(vocMeaningMapper.selectById(id).getVocId()).getContent());
        // 删除单词释义
        LambdaUpdateWrapper<VocMeaningDO> vocMeaningWrapper = new LambdaUpdateWrapper<>();
        vocMeaningWrapper.eq(VocMeaningDO::getId, id);
        int delete = vocMeaningMapper.delete(vocMeaningWrapper);
        if (delete > 0) {
            return dto;
        }
        return null;
    }

    @Override
    public DeleteVocExampleOperateDTO deleteExample(Long id) {

        DeleteVocExampleOperateDTO dto = new DeleteVocExampleOperateDTO();
        LambdaUpdateWrapper<VocChapterDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VocChapterDO::getId, id);
        dto.setId(id);
        dto.setSentence(exampleSentenceMapper.selectById(id).getContent());
        dto.setVocId(exampleSentenceMapper.selectById(id).getVocId());
        dto.setVocName(vocMapper.selectById(exampleSentenceMapper.selectById(id).getVocId()).getContent());

        // 删除单词例句
        LambdaUpdateWrapper<ExampleSentenceDO> exampleSentenceWrapper = new LambdaUpdateWrapper<>();
        exampleSentenceWrapper.eq(ExampleSentenceDO::getId, id);
        int delete = exampleSentenceMapper.delete(exampleSentenceWrapper);

        if (delete > 0) {
            return dto;
        }
        return null;

    }

    @Override
    public String uploadVocabularyFile(MultipartFile file) throws IOException {
        String nextFileName = FileNameGenerator.nextFileName(file.getOriginalFilename());
        String finalFileName = "voice/" + nextFileName;
        oss.putObject(aliOssProperties.getBucketName(), finalFileName, file.getInputStream());
        // 文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder.append(aliOssProperties.getBucketName())
                .append(".")
                .append(aliOssProperties.getEndpoint())
                .append("/")
                .append(finalFileName);

        System.out.println(stringBuilder.toString());

        return stringBuilder.toString();
    }
}
